package com.itheima.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.constant.TokenConstant;
import com.itheima.domain.AddressBook;
import com.itheima.domain.User;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public R<String> clientSetDefaultAddress(String token, AddressBook addressBook) {
        String userId = getUserIdByRedis(token);
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1);
        // 把之前的默认地址改成0
        AddressBook addressBookBefore = getOne(lqw);
        addressBookBefore.setIsDefault(0);
        updateById(addressBookBefore);
        // 把该地址设置成默认地址
        addressBook.setIsDefault(1);

        boolean result = updateById(addressBook);

        if (result) {
            return R.success("设置成功");
        }

        return R.error("设置失败");
    }
    @Override
    public R<AddressBook> clientGetDefaultAddress(String token) {

        String userId = getUserIdByRedis(token);

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = getOne(lqw);

        if (addressBook == null) {
            return R.error("查询错误");
        }
        return R.success(addressBook);
    }
    @Override
    public R<AddressBook> clientAddAddress(String token, AddressBook addressBook) {
        String userId = getUserIdByRedis(token);
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        boolean result = save(addressBook);

        if (result) {
            return R.success(addressBook);
        }
        return R.error("添加错误");
    }
    @Override
    public R<List<AddressBook>> clientGetAddressList(String token) {
        String userId = getUserIdByRedis(token);
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);

        List<AddressBook> addressBookList = list(lqw);
        if (addressBookList == null) {
            return R.error("查询地址失败");
        }
        return R.success(addressBookList);
    }
    @Override
    public R<AddressBook> clientGetAddressById(String id) {
        // 通过地址id获取唯一的地址
        AddressBook addressBook = getById(id);

        return R.success(addressBook);
    }
    @Override
    public R<String> clientUpdateAddress(AddressBook addressBook) {
        boolean result = updateById(addressBook);
        if (result) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @Override
    public R<String> clientDeleteAddressById(String id) {
        boolean deleteResult = removeById(id);
        if (deleteResult) {
            return R.success("删除成功");
        }
        return R.error("删除失败");

    }


    private String getUserIdByRedis(String token) {
        String userStr = (String) redisTemplate.opsForValue().get(TokenConstant.USER_TOKEN_PREFIX + token);
        User user = JSON.parseObject(userStr, User.class);
        assert user != null;
        return user.getId();
    }

}
